package com.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 用来连接阿里云的短信
 */
public class AliyunCode {
    private static String AccessKeyId = "LTAI4FnRinE5JQTL6338uMmd";//对应的accesskeyid
    private static String AccessKeySecret = "wHJlPgloqex9sDrLjjUtfrrnFf7jXV";//对应的accesskeysecret

    public static void sendMessage(String telno, String code){  //传入手机号，一次调用支持发送一个手机号验证码
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKeyId, AccessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");//日期别乱写
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", telno);//手机号
        request.putQueryParameter("SignName", "饭团账号");//签名名称

        request.putQueryParameter("TemplateCode", "SMS_186598512");//模板code
       request.putQueryParameter("TemplateParam","{\"code\":"+code+"}");//这个里面填写验证码内容
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } /*catch (ServerException e) {
            e.printStackTrace();
        }*/ catch (ClientException e) {
            e.printStackTrace();
        }
    }

  /*  public static void main(String[] args) {
        sendMessage("15856987353");
    }*/
}

